package com.myway.service.ibe.command;

import com.myway.core.execption.IBEException;
import com.myway.dto.common.JsonData;
import com.myway.dto.logs.DTOIBELogs;
import com.myway.dto.logs.domain.IBEMethodType;
import com.myway.utils.DateUtils;
import com.travelsky.ibe.client.pnr.ETDZ;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/15.
 * 电子客票出票
 */
@Slf4j
public class IssueTicketCommand extends BaseIBECommand {
    private static Map<String,Integer> printers=new HashMap<String,Integer>();

    static{
        printers.put("MJ", 1);
    }
    public String execute(Map<String, Object> parms, String airline) {
        DTOIBELogs ibeETDZLog = new DTOIBELogs();
        int printer=1;
        if(printers.containsKey(airline)){
            printer=printers.get(airline);
        }
        String PNRNo=(String)parms.get("PNRNo");

        //是否国际票标识
        int international=0;
        if(parms.get( "international" )!=null)
        {
//            international =(Integer)parms.get( "international" );
        }
        //IBE出票返回信息
        String returnString = "OK";
        ETDZ dz=new ETDZ();
        this.configIBEClient(dz, airline,ibeETDZLog,international);
        long starttime = System.currentTimeMillis();
        try{
            int i=0;
            while(i<3) {
                try {
                    //从配置里读取打票机号
                    if(ibeETDZLog.getPrintNo()!= null && ibeETDZLog.getPrintNo() != ""){
                        returnString = dz.issueTicket(PNRNo, Integer.parseInt( ibeETDZLog.getPrintNo() ));
                    }else{
                        returnString = dz.issueTicket(PNRNo, printer);
                    }
                    break;
                } catch (Exception e) {
                    if(e.getMessage().contains("connect")||e.getMessage().contains("timed out")){
                        i++;
                        log.debug("出票重试："+i);
                        if(i==3)throw new IBEException(e);
                    }else{
                        throw new IBEException(e);
                    }
                }
            }
            log.info("PNR 出票"+returnString);
            if(!returnString.trim().toUpperCase().equals("OK")){
                throw new IBEException(returnString);
            }
        }catch (Exception e){
            //如果返回的错误信息包含：PNR TICKETED，表示PNR已经出票，则此处不记日志，不抛出异常。
            if(null == e.getMessage() ||e.getMessage().toUpperCase().indexOf("PNR TICKETED") == -1) {
                try{
                    log.info("\001"+"MARSISSUE1\002"
                            + DateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss")+"\002"
                            +"255.255.255.255\002"
                            +"MTISSUE001\002"
                            +PNRNo +"\002"
                            +airline  +"\002"
                            +"MARS\002"
                            +printer +"\002"
                            +"N/A\002"
                            +"500\002"
                            +e.getMessage()+"\002"
                            +"N/A\002"
                            +"N/A\002"
                            +"\001");
                }catch(Exception e1){e1.printStackTrace();}
                ibeETDZLog.setCreateTime( new Date() );
                ibeETDZLog.setPnrNo( PNRNo );
                ibeETDZLog.setIbeMethod( IBEMethodType.ISSUETICKET );
                ibeETDZLog.setContent("PNR:"+PNRNo+" 出票时候报错，出错信息:"+e.getMessage());
                //记录IBE Issue 操作日志
                //IBELogsService ibeLogsService = (IBELogsService)SpringBeanUtil.lookup("ibeLogsService");
                //保存
                //ibeLogsService.addIbeLogs( ibeETDZLog );
                //使用异布日志===========================================================
//                sendLog(airline, ibeETDZLog);
                log.error(e.getMessage(),e);
                throw new IBEException(e.getMessage(),e);
            }
        }

        ibeETDZLog.setCreateTime( new Date() );
        ibeETDZLog.setPnrNo( PNRNo );
        ibeETDZLog.setIbeMethod( IBEMethodType.ISSUETICKET );
        ibeETDZLog.setContent("PNR:"+PNRNo+" 出票成功！");
        return returnString;
        //保存
//		ibeLogsService.addIbeLogs( ibeETDZLog );
    }
}
